/**
 * There are <a href="https://www.hdzhx.com">邯郸智讯</a> code generation
 */
package com.boxin.ims.modules.wechat.entity;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.validator.constraints.Length;

import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.thinkgem.jeesite.common.persistence.BaseEntity;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * 中奖人记录Entity
 * @author Jakemanse
 * @version 2013-09-23
 */
@Entity
@Table(name = "wp_lottery_record")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class LotteryRecord extends BaseEntity {
	
	public final static Integer SOURCE_TYPE_ME = 1;		//
	public final static Integer SOURCE_TYPE_SELLER = 2;	//商家抽奖
	
	public final static Integer SURCE_TYPE_REG=3;		//注册会员送奖品
	
	public final static Integer SCOPE_NEW = 0;		//新注册会员
	public final static Integer SCOPE_SCENE = 1;		//现场被动抽奖
	public final static Integer SCOPE_OUT_SCENE = 2;		//场外被动抽奖
	public final static Integer SCOPE_ALL = 3;		//所有会员
	
	
	
	private static final long serialVersionUID = 1L;
	
	private Long id; 		// 编号
	private User user; 		// 用户
	private Lottery lottery;		//对应的抽奖项
	private WechatMember member;	//微信会员
	private QRCode	qrCode;		//对应的二维码
	private Integer sourceType;	//中奖来源  0：主动抽奖  1：商家抽奖
	private Integer scope;		//0：新注册 1：现场人员  2：场外人员 3：所有会员
	private String name; 	// 名称	//奖项名称
	private Integer score;		//领取的积分分值
	private String status;	 //状态 0:未使用  1：已使用  2:作废   3：过期作废
	private Date createTime;	// 创建日期
	private Date endTime;		//失效时间
	private Date updateTime ;	//核销时间
	
	private String delFlag;	// 删除标记（0：正常；1：删除）
	private String remarks; // 备注
	
	@Transient
	private Date startTime;	
	
	private boolean valid;	//是否过期

	public LotteryRecord() {
		this.score = 0;
		this.createTime = new Date();
		this.delFlag = DEL_FLAG_NORMAL;
	}

	public LotteryRecord(Long id){
		this();
		this.id = id;
	}
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	//@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_wechat_lotteryRecord")
	//@SequenceGenerator(name = "seq_wechat_lotteryRecord", sequenceName = "seq_wechat_lotteryRecord")
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@ManyToOne
	@JoinColumn(name="user_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@NotNull
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	@Length(min=1, max=200)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Length(min=0, max=255)
	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}
	
	@NotNull
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Length(min=1, max=1)
	public String getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(String delFlag) {
		this.delFlag = delFlag;
	}

	@ManyToOne
	@JoinColumn(name="lottery_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public Lottery getLottery() {
		return lottery;
	}

	public void setLottery(Lottery lottery) {
		this.lottery = lottery;
	}

	@ManyToOne
	@JoinColumn(name="member_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@NotNull
	public WechatMember getMember() {
		return member;
	}

	public void setMember(WechatMember member) {
		this.member = member;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	/**
	 * @author Jakemanse
	 * @time 2013-10-1  上午7:32:16
	 * @function <p>是否过期  </p>
	 * @return
	 */
	@Transient
	public boolean isValid() {
		java.util.Calendar c1=java.util.Calendar.getInstance();
		java.util.Calendar c2=java.util.Calendar.getInstance();
		try {
			c1.setTime(new Date());
			c2.setTime(endTime);
		} catch (Exception e) {
			System.err.println("格式不正确");
			return true;
		}
		int result = c1.compareTo(c2);
		if (result > 0){	//c1>c2  过期
			return false;
		}
		
//		Date nowDate = new Date();
//		if(nowDate.getTime() > endTime.getTime()){
//			return false;
//		}
		return true;
		
	}

	public void setValid(boolean valid) {
		this.valid = valid;
	}
	
	
	
	@ManyToOne
	@JoinColumn(name="qrcode_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@NotNull
	public QRCode getQrCode() {
		return qrCode;
	}

	public void setQrCode(QRCode qrCode) {
		this.qrCode = qrCode;
	}

	public Integer getSourceType() {
		return sourceType;
	}

	public void setSourceType(Integer sourceType) {
		this.sourceType = sourceType;
	}

	public Integer getScope() {
		return scope;
	}

	public void setScope(Integer scope) {
		this.scope = scope;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	@Transient
	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Integer getScore() {
		return score==null ?0:score;
	}

	public void setScore(Integer score) {
		this.score = score;
	}
	
	
	
	
	
	
	
}


